<template>
  <div class="pagination">
    <button :disabled="pageNo === 1" @click="changpage(pageNo - 1)">
      上一页
    </button>
    <button v-show="startEnd.start > 1" @click="changpage(1)">1</button>
    <button v-show="startEnd.start > 2">···</button>

    <button
      v-for="page in startEnd.end"
      v-if="page >= startEnd.start"
      :class="{ active: page === pageNo }"
      :key="page"
      @click="changpage(page)"
    >
      {{ page }}
    </button>

    <button v-show="startEnd.end < totalPages - 1">···</button>
    <button v-show="startEnd.end < totalPages" @click="changpage(totalPages)">
      {{ totalPages }}
    </button>
    <button :disabled="pageNo === totalPages" @click="changpage(pageNo + 1)">
      下一页
    </button>

    <button style="margin-left: 30px">共 {{total}} 条</button>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
export default {
  name: "Pagination",
  props: {
    totalPages: Number,
    pageNo: Number,
    pageSize: Number,
    continueNum: Number,
    total:Number
  },
  methods: {
    changpage(page) {
      this.$emit("changPage", page);
    },
  },
  computed: {
    //...mapGetters(["total"]),
    startEnd() {
      const { continueNum, totalPages, pageNo } = this;
      let start = 0;
      let end = 0;
      //判断连续页是否大于总页数
      if (continueNum > totalPages) {
        start = 1;
        end = totalPages;
      } else {
        start =
          pageNo -
          (continueNum % 2 == 0 ? continueNum / 2 - 1 : (continueNum - 1) / 2);
        end =
          pageNo +
          (continueNum % 2 == 0 ? continueNum / 2 : (continueNum - 1) / 2);
        if (start < 1) {
          start = 1;
          end = continueNum;
        }
        if (end > totalPages) {
          end = totalPages;
          start = totalPages - continueNum + 1;
        }
      }
      return {
        start,
        end,
      };
    },
  },
};
</script>

<style lang="less" scoped>
.pagination {
  width: 660px;
  margin: auto;
  button {
    margin: 0 5px;
    background-color: #f4f4f5;
    color: #606266;
    outline: none;
    border-radius: 2px;
    padding: 0 4px;
    vertical-align: top;
    display: inline-block;
    font-size: 13px;
    min-width: 35.5px;
    height: 28px;
    line-height: 28px;
    cursor: pointer;
    box-sizing: border-box;
    text-align: center;
    border: 0;

    &[disabled] {
      color: #c0c4cc;
      cursor: not-allowed;
    }

    &.active {
      cursor: not-allowed;
      background-color: #409eff;
      color: #fff;
    }
  }
}
</style>